<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="我的vue音乐播放器实战, 谢无痕,博客">
    <meta name="description" content="个人学习和博客的记录分享网站">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>我的vue音乐播放器实战 | 无痕客栈</title>
    <link rel="icon" type="image/png" href="/wuhen/favicon.png">

    <link rel="stylesheet" type="text/css" href="/wuhen/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/wuhen/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/wuhen/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/wuhen/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/wuhen/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/wuhen/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/wuhen/css/my.css">

    <script src="/wuhen/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 4.2.0"></head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/wuhen/" class="waves-effect waves-light">
                    
                    <img src="/wuhen/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">无痕客栈</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/wuhen/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/wuhen/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/wuhen/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/wuhen/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/wuhen/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/wuhen/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/wuhen/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/wuhen/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">无痕客栈</div>
        <div class="logo-desc">
            
            个人学习和博客的记录分享网站
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/wuhen/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/wuhen/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/wuhen/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/wuhen/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/wuhen/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/wuhen/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/wuhen/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/XieWuHen/XieWuHen.github.io" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/XieWuHen/XieWuHen.github.io" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/wuhen/medias/featureimages/17.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">我的vue音乐播放器实战</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/wuhen/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        height: calc(100vh - 250px);
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/wuhen/tags/Vue/">
                                <span class="chip bg-color">Vue</span>
                            </a>
                        
                            <a href="/wuhen/tags/%E9%9F%B3%E4%B9%90%E6%92%AD%E6%94%BE%E5%99%A8/">
                                <span class="chip bg-color">音乐播放器</span>
                            </a>
                        
                            <a href="/wuhen/tags/%E5%AE%9E%E6%88%98/">
                                <span class="chip bg-color">实战</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2020-07-03
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2020-09-04
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    1.4k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    5 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h4 id="浏览地址："><a href="#浏览地址：" class="headerlink" title="浏览地址："></a>浏览地址：</h4><p><a href="http://163.53.170.82:5001/wuhen_music/" target="_blank" rel="noopener">http://163.53.170.82:5001/wuhen_music/</a><br>注意请用手机打开</p>
<h4 id="源码："><a href="#源码：" class="headerlink" title="源码："></a>源码：</h4><p><a href="https://github.com/XieWuHen/web-music" target="_blank" rel="noopener">https://github.com/XieWuHen/web-music</a></p>
<h4 id="使用技术栈"><a href="#使用技术栈" class="headerlink" title="使用技术栈"></a>使用技术栈</h4><ul>
<li>vue</li>
<li>vue-axios</li>
<li>vue-router</li>
<li>better-scroll</li>
<li>swiper</li>
</ul>
<h4 id="后台数据来源"><a href="#后台数据来源" class="headerlink" title="后台数据来源"></a>后台数据来源</h4><ul>
<li>网易云api: <a href="https://binaryify.github.io/NeteaseCloudMusicApi/#/?id=neteasecloudmusicapi" target="_blank" rel="noopener">https://binaryify.github.io/NeteaseCloudMusicApi/#/?id=neteasecloudmusicapi</a></li>
</ul>
<hr>
<p>####特别说明，歌手页面由于网易云api提供的歌手头像图片都太大，没得选，几乎每张都2M左右，所以我不显示图片了，要不然非常慢和卡顿，不知道设计api的大大怎么个想法，吐槽一下，还有其它的图片资源也都是很大体积的，还没得选，每张图片都1M以上，实在蛋疼，还有api返回数据及其繁杂啰嗦，一层套着一层有时套了n层看的头皮发麻，有机会我自己爬网易云api自己造接口（然并卵）哼哼</p>
<hr>
<h4 id="歌手页面的歌手名字按首字母排序及首字母滚动导航功能："><a href="#歌手页面的歌手名字按首字母排序及首字母滚动导航功能：" class="headerlink" title="歌手页面的歌手名字按首字母排序及首字母滚动导航功能："></a>歌手页面的歌手名字按首字母排序及首字母滚动导航功能：</h4><hr>
<h5 id="按首字母排序"><a href="#按首字母排序" class="headerlink" title="按首字母排序:"></a>按首字母排序:</h5><p>这里需要用到”js-pinyin”这个插件，npm安装好它，它能够将中文转成拼音，然后截取第一个字符即为首字母，然后使用 oldList.sort(a,b) 方法 return a.localeCompare(b) 进行 Unicode 编码比较大小并排序，得到新的排序好的数据newList </p>
<h5 id="首字母滚动导航"><a href="#首字母滚动导航" class="headerlink" title="首字母滚动导航:"></a>首字母滚动导航:</h5><p>先提取 oldList 每位歌手名字首字母并大写，其它特殊字符的统一分配到 * 类别里（包括日语韩语特殊字符等等）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> reg=<span class="regexp">/^[a-zA-Z]+$/</span>; <span class="comment">//26个字母</span></span><br><span class="line">artists.forEach(<span class="function">(<span class="params">val, index</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">let</span> firstChar = pinyin.getCamelChars(val.name.slice(<span class="number">0</span>,<span class="number">1</span>));</span><br><span class="line">  <span class="keyword">if</span>(!reg.test(firstChar))&#123;</span><br><span class="line">    firstChar = <span class="string">'*'</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">this</span>.sortList.push(firstChar);</span><br><span class="line">  artists[index].tag = firstChar;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// 去除重复</span></span><br><span class="line"><span class="keyword">this</span>.sortList = <span class="keyword">new</span> <span class="built_in">Set</span>(<span class="keyword">this</span>.sortList);</span><br><span class="line"><span class="keyword">this</span>.sortList = [...this.sortList];</span><br><span class="line"><span class="keyword">this</span>.sortList.forEach(<span class="function"><span class="params">val</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">//这里给每个歌手数据价格tag标签当成导航标记，便于导航跳转</span></span><br><span class="line">  <span class="keyword">this</span>.list.push(&#123;</span><br><span class="line">  tag: val,</span><br><span class="line">  data: artists.filter(<span class="function"><span class="params">item</span> =&gt;</span> item.tag == val)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>为每一个tab添加点击事件：<br>@click=”toTag(index)”</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">toTag(index)&#123;</span><br><span class="line">  <span class="comment">// console.log('屏幕滚动到相应的索引值：'+index);</span></span><br><span class="line">  <span class="keyword">let</span> el = <span class="built_in">document</span>.getElementsByClassName(<span class="string">"group"</span>)[index];</span><br><span class="line">  <span class="keyword">this</span>.$refs.myScroll.scrollToElement(el, <span class="number">1000</span>, <span class="number">0</span>, <span class="number">-40</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="解析歌词："><a href="#解析歌词：" class="headerlink" title="解析歌词："></a>解析歌词：</h4><hr>
<p>创建一个工具js插件（dealLyrcics.js），网易云歌词数据是字符串格式的，所以我们需要对其进行解析成数组，分别对应为时间轴数组和歌词数组。歌词数据是有规律的，时间都用 [] 包裹起来，所以分别用 str.split(‘[‘) 和 value.split(‘]’) 将[]全部去掉并分割为数组，即： [00:00:01]一二三四五\n[00:00:12]五四三二一\n 变成数组 arry = [‘00:00:01’, ‘一二三四五\n’, ‘00:00:12’, ‘五四三二一\n’]，最后分别将奇数下标的数据放在时间数组arry1 = [‘00:00:01’, ‘00:00:12’]，将偶数小标的数据放在对应的歌词数组arry2 = [‘一二三四五\n’, ‘五四三二一\n’]<br>时间轴数组是为了实时匹配音乐播放时间轴的，即音乐播放到某某时间轴时就到了哪个对应时间轴的歌词高亮了。</p>
<h4 id="歌词音乐同步滚动功能（难点）："><a href="#歌词音乐同步滚动功能（难点）：" class="headerlink" title="歌词音乐同步滚动功能（难点）："></a>歌词音乐同步滚动功能（难点）：</h4><hr>
<p>监听音乐文件时间变化，然后歌词页面用watch监听store里音乐时间的变化，并判断歌词数组中时间点是否在当前音乐播放时间的0.4s内（即大于当前播放时间-0.2s且小于当前播放时间+0.2s），在条件OK下再执行滚动和样式改变</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 监听音乐文件播放时间变化：</span></span><br><span class="line">music.addEventListener(<span class="string">"timeupdate"</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="comment">// 这里将获取到的音乐当前播放的时长存到vuex的store里去共享</span></span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// 歌词页面监听vuex里的音乐时间状态变化：</span></span><br><span class="line">watch: &#123;</span><br><span class="line">  songControl: &#123;</span><br><span class="line">    deep: <span class="literal">true</span>,</span><br><span class="line">    handler(val)&#123;</span><br><span class="line">      <span class="keyword">if</span>(<span class="keyword">typeof</span>(<span class="keyword">this</span>.lyrObj.time) === <span class="string">'undefined'</span>)&#123;<span class="keyword">return</span>;&#125;</span><br><span class="line">      <span class="comment">// 遍历一遍，怕有的歌词时间点间隔太小监听的timeupdate会错过导致出错，所以便利来设定此时的index下标</span></span><br><span class="line">      <span class="keyword">this</span>.lyrObj.time.forEach(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span>(item &lt;= val.curTime)&#123;</span><br><span class="line">          <span class="keyword">this</span>.lyrIndex = index;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;);</span><br><span class="line">      <span class="comment">// console.log(val.curTime);</span></span><br><span class="line">      <span class="keyword">if</span>(<span class="keyword">this</span>.lyrObj.time[<span class="keyword">this</span>.lyrIndex] &gt;= val.curTime<span class="number">-0.2</span> &amp;&amp; <span class="keyword">this</span>.lyrObj.time[<span class="keyword">this</span>.lyrIndex] &lt;= val.curTime+<span class="number">0.2</span>)&#123;</span><br><span class="line">        <span class="comment">// console.log('下一条歌词来了');</span></span><br><span class="line">        <span class="keyword">this</span>.lyrRun();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 歌词滚动及样式函数：</span></span><br><span class="line">lyrRun()&#123;</span><br><span class="line">  <span class="keyword">let</span> boxH = <span class="keyword">this</span>.$refs.context.offsetHeight;</span><br><span class="line">  <span class="keyword">let</span> content = <span class="keyword">this</span>.$refs.lyrBox;</span><br><span class="line">  <span class="keyword">let</span> height = content.offsetHeight;</span><br><span class="line">  <span class="comment">// console.log('||');</span></span><br><span class="line">  <span class="keyword">if</span>(height == <span class="number">0</span>)&#123;<span class="keyword">return</span>;&#125;</span><br><span class="line">  <span class="keyword">this</span>.cssIndex = <span class="keyword">this</span>.lyrIndex; <span class="comment">//item中的index=cssIndex时样式改变</span></span><br><span class="line">  <span class="comment">//向上滚动：</span></span><br><span class="line">  <span class="keyword">let</span> curTop = <span class="keyword">this</span>.$refs[<span class="string">`lyrItem<span class="subst">$&#123;<span class="keyword">this</span>.lyrIndex&#125;</span>`</span>][<span class="number">0</span>].offsetTop - (boxH/<span class="number">2</span>);</span><br><span class="line">  content.style.cssText = <span class="string">`transform: translateY(<span class="subst">$&#123;-curTop&#125;</span>px)`</span>;</span><br><span class="line">  </span><br><span class="line">&#125;,</span><br><span class="line"><span class="comment">// 这里的curTop值计算公式为：</span></span><br><span class="line"><span class="comment">// X + 容器或可视窗口高度/2 = 当前索引的这句歌词所在元素的offsetTop</span></span><br><span class="line"><span class="comment">// 求这个 X 就是curTop</span></span><br><span class="line"><span class="comment">// 这会保证当前激活/时间点的歌词始终保持在容器可视区域Y轴的中间位置</span></span><br></pre></td></tr></table></figure>
<p>#####当前问题和待改善：<br>由于v-show会让歌词页面没显示的时候所有元素的宽高都为0，所以我只能实时改变承载着歌词item的父元素lyrBox的transform: translateY()来实现滚动，这样就不会在隐藏歌词页面时所以变量变成0之后的诸多麻烦，反正再显示歌词页面时它又会实时的滚动到正确位置，也就是这样会导致刚显示歌词页面时候会看到0.5秒的从0滚动到当前歌词的位置，而不是由上一句歌词位置滚动一点点到当前时间点的歌词位置。</p>
<hr>

            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/wuhen/about" rel="external nofollow noreferrer">谢无痕</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://xj-work.gitee.io/wuhen/wuhen/2020/07/03/%E6%88%91%E7%9A%84vue%E9%9F%B3%E4%B9%90%E6%92%AD%E6%94%BE%E5%99%A8%E5%AE%9E%E6%88%98/">http://xj-work.gitee.io/wuhen/wuhen/2020/07/03/%E6%88%91%E7%9A%84vue%E9%9F%B3%E4%B9%90%E6%92%AD%E6%94%BE%E5%99%A8%E5%AE%9E%E6%88%98/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/wuhen/about" target="_blank">谢无痕</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/wuhen/tags/Vue/">
                                    <span class="chip bg-color">Vue</span>
                                </a>
                            
                                <a href="/wuhen/tags/%E9%9F%B3%E4%B9%90%E6%92%AD%E6%94%BE%E5%99%A8/">
                                    <span class="chip bg-color">音乐播放器</span>
                                </a>
                            
                                <a href="/wuhen/tags/%E5%AE%9E%E6%88%98/">
                                    <span class="chip bg-color">实战</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/wuhen/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/wuhen/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/wuhen/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/wuhen/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/wuhen/2020/09/23/js%E7%9B%B8%E5%85%B3-%E4%BA%8B%E4%BB%B6%E5%86%92%E6%B3%A1-%E8%AE%B0%E5%BD%95%E4%B8%80%E6%AC%A1%E8%9B%8B%E7%96%BC%E7%9A%84%E8%AF%AF%E4%BC%9A/">
                    <div class="card-image">
                        
                        
                        <img src="/wuhen/medias/featureimages/15.jpg" class="responsive-img" alt="js相关 || 事件冒泡-记录一次蛋疼的误会">
                        
                        <span class="card-title">js相关 || 事件冒泡-记录一次蛋疼的误会</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                              项目中频繁遇到一些 dom 的手势操作，需要给 dom 绑定监听事件，这个时候就用到了事件冒泡，而有时候确不想要事件冒泡，所以网上几乎所有方法都是 event.stopPropagation() 这里的 event 是 PointerE
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2020-09-23
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            谢无痕
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/wuhen/tags/js/">
                        <span class="chip bg-color">js</span>
                    </a>
                    
                    <a href="/wuhen/tags/%E9%97%AE%E9%A2%98/">
                        <span class="chip bg-color">问题</span>
                    </a>
                    
                    <a href="/wuhen/tags/%E4%BA%8B%E4%BB%B6%E5%86%92%E6%B3%A1/">
                        <span class="chip bg-color">事件冒泡</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/wuhen/2020/01/01/%E7%AC%AC%E4%B8%80%E4%B8%AA%E6%B5%8B%E8%AF%95%E6%96%87%E7%AB%A0/">
                    <div class="card-image">
                        
                        
                        <img src="/wuhen/medias/featureimages/17.jpg" class="responsive-img" alt="第一个测试文章">
                        
                        <span class="card-title">第一个测试文章</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            hello world测试成功的一页呢Oh my god ! Look this guy, he is so strong

                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-01-01
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            谢无痕
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/wuhen/tags/%E6%B5%8B%E8%AF%95/">
                        <span class="chip bg-color">测试</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/wuhen/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/wuhen/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/wuhen/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/wuhen/libs/codeBlock/codeShrink.js"></script>


<!-- 代码块折行 -->


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/wuhen/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/wuhen/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="503838841"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/wuhen/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            <span id="year">2020</span>
            <a href="/wuhen/about" target="_blank">谢无痕</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/XieWuHen" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:xj601489641@163.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2973654054" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2973654054" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/wuhen/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/wuhen/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/wuhen/search.xml", 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/wuhen/libs/materialize/materialize.min.js"></script>
    <script src="/wuhen/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/wuhen/libs/aos/aos.js"></script>
    <script src="/wuhen/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/wuhen/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/wuhen/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/wuhen/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/wuhen/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    

    

    

    
    <script src="/wuhen/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
